package cn.cdeden.knowledge.utils;

import cn.cdeden.common.core.utils.StringUtils;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.FilesCollection;
import cn.cdeden.knowledge.domain.PersonalFilesTree;
import cn.cdeden.knowledge.domain.TeamFilesTree;
import cn.cdeden.knowledge.domain.dto.FilesCollectionQueryParam;
import cn.cdeden.knowledge.domain.dto.TeamDto;
import cn.cdeden.knowledge.mapper.FilesCollectionMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
//import com.cdyd.ecm.base.QueryHelpMybatisPlus;
//import com.cdyd.ecm.modules.enterpriseDoc.collection.domain.FilesCollection;
//import com.cdyd.ecm.modules.enterpriseDoc.collection.dto.FilesCollectionQueryParam;
//import com.cdyd.ecm.modules.enterpriseDoc.collection.mapper.FilesCollectionMapper;
//import com.cdyd.ecm.modules.enterpriseDoc.enterprise.domain.EnterpriseFilesTree;
//import com.cdyd.ecm.modules.personDoc.domain.PersonalFilesTree;
//import com.cdyd.ecm.modules.teamDoc.team.domain.TeamFilesTree;
//import com.cdyd.ecm.modules.teamDoc.team.dto.TeamDto;
//import com.cdyd.ecm.utils.FileUserUtils;
//import com.cdyd.ecm.utils.StringUtils;
import com.google.common.collect.Lists;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SetCollectionDataUtil {
    private final FilesCollectionMapper filesCollectionMapper;

    public SetCollectionDataUtil(FilesCollectionMapper filesCollectionMapper) {
        this.filesCollectionMapper = filesCollectionMapper;
    }


    /**
     * @Description // 根据条件查询信息
     * @Date 2023/11/8 19:13
     * @param fileType
     * @param id
     * @param ids
     * @return java.util.List<com.cdyd.ecm.modules.collection.domain.FilesCollection>
     **/
    private List<FilesCollection> selectList(String fileType, Long id , List<Long> ids){
        FilesCollectionQueryParam query = new FilesCollectionQueryParam();
        QueryWrapper<FilesCollection> predicate = QueryHelpMybatisPlus.getPredicate(query);
//        Long id = FileUserUtils.getUser().getId();
        predicate.eq("file_type",fileType);
        predicate.eq("user_id", id);
        if (ids.size() > 1000){
            predicate.in("tree_node_id",ids);
        }else {
            List<List<Long>> list = Lists.partition(ids, 1000);
            for (List<Long> longs : list) {
                predicate.or().in("tree_node_id",longs);
            }

        }
//        List<FilesCollection> list =
        return  filesCollectionMapper.selectList(predicate);
    }




    /**
     * @Description //   个人库获取收藏
     * @Date 2023/11/8 19:03
     * @param personalFilesTrees
     * @return java.util.List<com.cdyd.ecm.modules.personal.domain.PersonalFilesTree>
     **/
    public List<PersonalFilesTree> setCollectionPersonalData(List<PersonalFilesTree> personalFilesTrees) {
        if (CollectionUtils.isNotEmpty(personalFilesTrees)) {
            List<Long> ids = personalFilesTrees.stream().map(PersonalFilesTree::getId).collect(Collectors.toList());
//            Long id = FileUserUtils.getUser().getId();
            Long id = LoginHelper.getUserId();
            List<FilesCollection> selectList = selectList("0", id, ids);
            if (CollectionUtils.isNotEmpty(selectList)){
                Map<String, FilesCollection> collectionMap = new HashMap<>();
                for (FilesCollection collection : selectList) {
                    collectionMap.put(collection.getTreeNodeId(), collection);
                }

                for (PersonalFilesTree tree : personalFilesTrees) {
                    String treeId = String.valueOf(tree.getId());
                    // 检查映射中是否存在匹配的treeNodeId
                    if (collectionMap.containsKey(treeId)) {
                        FilesCollection matchedCollection = collectionMap.get(treeId);
                        // 假设setCollection是已经定义好的方法
                        tree.setCollection("1");
                        // 这里不再需要从selectList中移除元素，因为已经通过映射提前准备
                    }else {
                        tree.setCollection("0");
                    }
                }
            }else {
                for (PersonalFilesTree tree : personalFilesTrees) {
                    tree.setCollection("0");
                }
            }
        }

        return personalFilesTrees;
    }




    /**
     * @Description // 团队
     * @Date 2023/11/8 19:39
     * @param teamFilesTrees
     * @return java.util.List<com.cdyd.ecm.modules.team.domain.TeamFilesTree>
     **/
    public List<TeamFilesTree> setCollectionTeamFileData(List<TeamFilesTree> teamFilesTrees) {
        if (ObjectUtils.isNotEmpty(teamFilesTrees)) {
            List<Long> ids = teamFilesTrees.stream().map(TeamFilesTree::getId).collect(Collectors.toList());
            Long id = LoginHelper.getUserId();
            List<FilesCollection> selectList = selectList("1", id, ids);

            if (ObjectUtils.isNotEmpty(selectList)){
                for (TeamFilesTree tree : teamFilesTrees) {
                    String treeId = String.valueOf(tree.getId());
                    if (selectList.size() == 0) {
                        break;
                    }
                    boolean b = true;
                    for (int i = 0; i < selectList.size(); i++) {
                        FilesCollection collection = selectList.get(i);
                        String treeNodeId = collection.getTreeNodeId();
                        if (StringUtils.equals(treeId,treeNodeId) && StringUtils.equals(collection.getUserId(),String.valueOf(id))) {
                            b = false;
                            tree.setCollection("1");
                            selectList.remove(i);
                            break;
                        }
                    }
                    if (b){
                        tree.setCollection("0");
                    }
                }
            }else {
                for (TeamFilesTree tree : teamFilesTrees) {
                    tree.setCollection("0");
                }
            }
        }
        return teamFilesTrees;
    }

//    public List<EnterpriseFilesTree> setCollectionEnterpriseData(List<EnterpriseFilesTree> enterpriseFilesTrees) {
//        for (int i = 0; i < enterpriseFilesTrees.size(); i++) {
//            FilesCollectionQueryParam query = new FilesCollectionQueryParam();
//            QueryWrapper<FilesCollection> predicate = QueryHelpMybatisPlus.getPredicate(query);
//            predicate.eq("file_type","2");
//            predicate.eq("user_id",FileUserUtils.getUser().getId());
//            predicate.eq("tree_node_id",enterpriseFilesTrees.get(i).getId());
//            boolean b = filesCollectionMapper.selectList(predicate).size() > 0;
//            enterpriseFilesTrees.get(i).setCollection(b?"1":"0");
//        }
//        return enterpriseFilesTrees;
//    }

//    /**
//     * @Description // 企业收藏
//     * @Date 2023/11/8 19:39
//     * @param enterpriseFilesTrees
//     * @param userId
//     * @return java.util.List<com.cdyd.ecm.modules.enterprise.domain.EnterpriseFilesTree>
//     **/
//    public List<EnterpriseFilesTree> setCollectionEnterpriseData(List<EnterpriseFilesTree> enterpriseFilesTrees,Long userId) {
////        for (int i = 0; i < enterpriseFilesTrees.size(); i++) {
////            FilesCollectionQueryParam query = new FilesCollectionQueryParam();
////            QueryWrapper<FilesCollection> predicate = QueryHelpMybatisPlus.getPredicate(query);
////            predicate.eq("file_type","2");
////            predicate.eq("user_id",userId);
////            predicate.eq("tree_node_id",enterpriseFilesTrees.get(i).getId());
////            boolean b = filesCollectionMapper.selectList(predicate).size() > 0;
////            enterpriseFilesTrees.get(i).setCollection(b?"1":"0");
////        }
////        return enterpriseFilesTrees;
//        if (ObjectUtils.isNotEmpty(enterpriseFilesTrees)) {
//            List<Long> ids = enterpriseFilesTrees.stream().map(EnterpriseFilesTree::getId).collect(Collectors.toList());
////            Long id = FileUserUtils.getUser().getId();
//            List<FilesCollection> selectList = selectList("1", userId, ids);
//
//            if (ObjectUtils.isNotEmpty(selectList)){
//                for (EnterpriseFilesTree tree : enterpriseFilesTrees) {
//                    String treeId = String.valueOf(tree.getId());
//                    boolean b = true;
//                    if (selectList.size() == 0) {
//                        break;
//                    }
//                    for (int i = 0; i < selectList.size(); i++) {
//                        FilesCollection collection = selectList.get(i);
//                        String treeNodeId = collection.getTreeNodeId();
//                        if (StringUtils.equals(treeId,treeNodeId)) {
//                            tree.setCollection("1");
//                            b = false ;
//                            selectList.remove(i);
//                            break;
//                        }
//                    }
//
//                    if (b){
//                        tree.setCollection("0");
//                    }
//
//                }
//            }else {
//                for (EnterpriseFilesTree tree : enterpriseFilesTrees) {
//                    tree.setCollection("0");
//                }
//            }
//        }
//
//        return enterpriseFilesTrees;
//    }

    public List<TeamDto> setCollectionTeamData(List<TeamDto> teams) {
        for (int i = 0; i < teams.size(); i++) {
            FilesCollectionQueryParam query = new FilesCollectionQueryParam();
            QueryWrapper<FilesCollection> predicate = QueryHelpMybatisPlus.getPredicate(query);
            predicate.eq("file_type","3");
            predicate.eq("user_id",LoginHelper.getUserId());
            predicate.eq("tree_node_id",teams.get(i).getId());
            boolean b = filesCollectionMapper.selectList(predicate).size() > 0;
            teams.get(i).setCollection(b?"1":"0");
        }
        return teams;
    }

}
